function [savings_ev_quadrant_wave3,savings_ev_quadrant_wave6,savings_ev_quadrant_wave7,savings_ev_cubic_wave3,savings_ev_cubic_wave6,savings_ev_cubic_wave7] = generate_results_cross_waves(sample,winter_prevuse,cost,bsimax,SMC,savedir,fbase)
% This function estimates the wave-specific savings from applying rules
% across waves. 

% Inputs: 
% (1) sample: cross-sectional dataset with electricity consumption, 
% covariates, and propensity scores
% (2) winter_prevuse: indicates whether baseline consumption is calculated 
% as the mean of consumption in winter months (Jan and Feb) or as the mean
% of specified pre-treatment periods.
% (3) cost: =1 represents cost savings; =0 represents kWh reduction 
% (4) bsimax: number of bootstraps 
% (5) SMC: =1 use social marginal cost; =0 use retail electricity price
% (6) savedir: directory to save the savings table 
% (7) fbase: string used to indicate the propensity score and baseline
% months specification for output table 

% Outputs: 
% (1) savings_ev_quadrant_wave3: savings from applying quadrant rules on 
% target wave 3
% (1) savings_ev_quadrant_wave6: savings from applying quadrant rules on
% target wave 6
% (1) savings_ev_quadrant_wave7: savings from applying quadrant rules on 
% target wave 7
% (1) savings_ev_cubic_wave3: savings from applying cubic rules on 
% target wave 3
% (1) savings_ev_cubic_wave6: savings from applying cubic rules on 
% target wave 6
% (1) savings_ev_cubic_wave7: savings from applying cubic rules on 
% target wave 7

%% Specify the private marginal cost used to calculate cost savings cost 
if (cost)
    tcost = 0.765; % cost of sending out letter for each household
else
    tcost = 0;
end

%% Calculate benchmark savings in terms of ATE and ATT
wave={3 6 7};

benchmark_savings_wave3=calculate_benchmark_savings(sample,wave{1},tcost,SMC,bsimax);
benchmark_savings_wave6=calculate_benchmark_savings(sample,wave{2},tcost,SMC,bsimax);
benchmark_savings_wave7=calculate_benchmark_savings(sample,wave{3},tcost,SMC,bsimax);

%% Calculate EWM quadrant savings
covariates={'income' 'size' 'vintage'};
sample_wave={3 6 7};
target_wave={3 6 7};

savings_ev_quadrant_income_36= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{1},target_wave{2},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_income_37= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{1},target_wave{3},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_income_63= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{2},target_wave{1},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_income_67= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{2},target_wave{3},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_income_73= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{3},target_wave{1},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_income_76= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{3},target_wave{2},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);

savings_ev_quadrant_size_36= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{1},target_wave{2},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_size_37= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{1},target_wave{3},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_size_63= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{2},target_wave{1},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_size_67= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{2},target_wave{3},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_size_73= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{3},target_wave{1},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_size_76= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{3},target_wave{2},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);

savings_ev_quadrant_vintage_36= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{1},target_wave{2},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_vintage_37= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{1},target_wave{3},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_vintage_63= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{2},target_wave{1},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_vintage_67= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{2},target_wave{3},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_vintage_73= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{3},target_wave{1},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_quadrant_vintage_76= generate_results_quadrant_external_validity(bsimax,sample,sample_wave{3},target_wave{2},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);

savings_ev_quadrant_wave3=[savings_ev_quadrant_income_63; savings_ev_quadrant_income_73; ...
    savings_ev_quadrant_size_63; savings_ev_quadrant_size_73; ...
    savings_ev_quadrant_vintage_63; savings_ev_quadrant_vintage_73];

savings_ev_quadrant_wave6=[savings_ev_quadrant_income_36; savings_ev_quadrant_income_76; ...
    savings_ev_quadrant_size_36; savings_ev_quadrant_size_76; ...
    savings_ev_quadrant_vintage_36; savings_ev_quadrant_vintage_76];

savings_ev_quadrant_wave7=[savings_ev_quadrant_income_37; savings_ev_quadrant_income_67; ...
    savings_ev_quadrant_size_37; savings_ev_quadrant_size_67; ...
    savings_ev_quadrant_vintage_37; savings_ev_quadrant_vintage_67];

%% Calculate EWM cubic savings 
covariates={'income' 'size' 'vintage'};
sample_wave={3 6 7};
target_wave={3 6 7};

savings_ev_cubic_income_36= generate_results_cubic_external_validity(bsimax,sample,sample_wave{1},target_wave{2},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_income_37= generate_results_cubic_external_validity(bsimax,sample,sample_wave{1},target_wave{3},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_income_63= generate_results_cubic_external_validity(bsimax,sample,sample_wave{2},target_wave{1},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_income_67= generate_results_cubic_external_validity(bsimax,sample,sample_wave{2},target_wave{3},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_income_73= generate_results_cubic_external_validity(bsimax,sample,sample_wave{3},target_wave{1},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_income_76= generate_results_cubic_external_validity(bsimax,sample,sample_wave{3},target_wave{2},tcost,covariates{1},winter_prevuse,SMC,savedir,fbase);

savings_ev_cubic_size_36= generate_results_cubic_external_validity(bsimax,sample,sample_wave{1},target_wave{2},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_size_37= generate_results_cubic_external_validity(bsimax,sample,sample_wave{1},target_wave{3},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_size_63= generate_results_cubic_external_validity(bsimax,sample,sample_wave{2},target_wave{1},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_size_67= generate_results_cubic_external_validity(bsimax,sample,sample_wave{2},target_wave{3},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_size_73= generate_results_cubic_external_validity(bsimax,sample,sample_wave{3},target_wave{1},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_size_76= generate_results_cubic_external_validity(bsimax,sample,sample_wave{3},target_wave{2},tcost,covariates{2},winter_prevuse,SMC,savedir,fbase);

savings_ev_cubic_vintage_36= generate_results_cubic_external_validity(bsimax,sample,sample_wave{1},target_wave{2},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_vintage_37= generate_results_cubic_external_validity(bsimax,sample,sample_wave{1},target_wave{3},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_vintage_63= generate_results_cubic_external_validity(bsimax,sample,sample_wave{2},target_wave{1},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_vintage_67= generate_results_cubic_external_validity(bsimax,sample,sample_wave{2},target_wave{3},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_vintage_73= generate_results_cubic_external_validity(bsimax,sample,sample_wave{3},target_wave{1},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);
savings_ev_cubic_vintage_76= generate_results_cubic_external_validity(bsimax,sample,sample_wave{3},target_wave{2},tcost,covariates{3},winter_prevuse,SMC,savedir,fbase);

savings_ev_cubic_wave3=[savings_ev_cubic_income_63; savings_ev_cubic_income_73; ...
    savings_ev_cubic_size_63; savings_ev_cubic_size_73; ...
    savings_ev_cubic_vintage_63; savings_ev_cubic_vintage_73];

savings_ev_cubic_wave6=[savings_ev_cubic_income_36; savings_ev_cubic_income_76; ...
    savings_ev_cubic_size_36; savings_ev_cubic_size_76; ...
    savings_ev_cubic_vintage_36; savings_ev_cubic_vintage_76];

savings_ev_cubic_wave7=[savings_ev_cubic_income_37; savings_ev_cubic_income_67; ...
    savings_ev_cubic_size_37; savings_ev_cubic_size_67; ...
    savings_ev_cubic_vintage_37; savings_ev_cubic_vintage_67];
%% Summarize savings 

Targetwaves3={'6';'7';'6';'7';'6';'7'};
Targetwaves6={'3';'7';'3';'7';'3';'7'};
Targetwaves7={'3';'6';'3';'6';'3';'6'};
savings_ev_quadrant_wave3=addvars(savings_ev_quadrant_wave3,Targetwaves3,'Before','covariate');
savings_ev_quadrant_wave6=addvars(savings_ev_quadrant_wave6,Targetwaves6,'Before','covariate');
savings_ev_quadrant_wave7=addvars(savings_ev_quadrant_wave7,Targetwaves7,'Before','covariate');

savings_ev_cubic_wave3=addvars(savings_ev_cubic_wave3,Targetwaves3,'Before','covariate');
savings_ev_cubic_wave6=addvars(savings_ev_cubic_wave6,Targetwaves6,'Before','covariate');
savings_ev_cubic_wave7=addvars(savings_ev_cubic_wave7,Targetwaves7,'Before','covariate');

benchmark_wave3_1=[{'Actual RCT'} {'Scaled ATT'} benchmark_savings_wave3{1,3}  ...
    benchmark_savings_wave3{1,4} 0 0 0];

benchmark_wave6_1=[{'Actual RCT'} {'Scaled ATT'} benchmark_savings_wave6{1,3}  ...
    benchmark_savings_wave6{1,4} 0 0 0];

benchmark_wave7_1=[{'Actual RCT'} {'Scaled ATT'} benchmark_savings_wave7{1,3}  ...
    benchmark_savings_wave7{1,4} 0 0 0];

benchmark_wave3= benchmark_wave3_1;
benchmark_wave3=array2table(benchmark_wave3,'VariableNames',{'Targetwaves3','covariate','percent','savings','totalsavings','cilb','ciub'});

benchmark_wave6= benchmark_wave6_1;
benchmark_wave6=array2table(benchmark_wave6,'VariableNames',{'Targetwaves6','covariate','percent','savings','totalsavings','cilb','ciub'});

benchmark_wave7= benchmark_wave7_1;
benchmark_wave7=array2table(benchmark_wave7,'VariableNames',{'Targetwaves7','covariate','percent','savings','totalsavings','cilb','ciub'});

savings_ev_quadrant_wave3=[benchmark_wave3; savings_ev_quadrant_wave3];
savings_ev_quadrant_wave6=[benchmark_wave6; savings_ev_quadrant_wave6];
savings_ev_quadrant_wave7=[benchmark_wave7; savings_ev_quadrant_wave7];

savings_ev_cubic_wave3=[benchmark_wave3; savings_ev_cubic_wave3];
savings_ev_cubic_wave6=[benchmark_wave6; savings_ev_cubic_wave6];
savings_ev_cubic_wave7=[benchmark_wave7; savings_ev_cubic_wave7];

end
